<?php
namespace app\api\controller\welfare;

use app\BaseController;

use think\facade\Db;


use app\models\user\User;
use app\models\user\UserDefinite;               //用户操作
use app\models\system\SystemGroupData;          //组合数据
use app\models\user\UserBasic;                  //用户基本信息

use learn\services\UtilService as Util;
use app\Request;

class IndexController extends BaseController
{
    //福利社信息
    public function index(Request $request)
    {
        //获取配置
        $quota = SystemConfigMore(['website_domain']);


        //获取菜单
        $menu = SystemGroupData::getAllValue("welfare_menus");
        foreach ($menu as $k=>$v) {
            $v['image'] = str_replace("\\","/",$quota['website_domain'] . $v['image']);
            $menu[$k] = $v;
        }

        //活动
        $activity = SystemGroupData::getAllValue("activity_list");
        foreach ($activity as $k=>$v) {
            $v['image'] = str_replace("\\","/",$quota['website_domain'] . $v['image']);
            $activity[$k] = $v;
        }

        $data = self::signRes($request->uid());

        $signList = $data['signList']; 

        //提现假数据
        $cashList = array();
        $fakeCash = getFakeCash();
        //对数据进行随机排序
        shuffle($fakeCash);

        for ($i=0;$i<count($fakeCash);$i++) {
            $cashList[] = $fakeCash[$i][0].'刚刚提现了'.$fakeCash[$i][1]."元";
        }
        //签到详情
        $signinfo = [
            'is_sign'   =>   $data['is_sign'],
            'count'     =>   $data['number'],
            'tomorrow'  =>   $data['tomorrow'],
            'month'     =>   Db::name("user_definite")->where("uid",$request->uid())->where("mold","sign")->where("type",0)->where("pm",1)->whereMonth("create_time")->sum("number")
        ];

        return app("json")->success(compact("menu","signList",'activity','signinfo','cashList'));
    }


    //签到详情
    public function signInfo(Request $request)
    {
        $uid = $request->uid();
        //获取配置
        $quota = SystemConfigMore(['website_domain']);

        $data = self::signRes($uid);
        $signList = $data['signList'];

        $signinfo = [
            'is_sign'   =>   $data['is_sign'],
            'count'     =>   $data['number'],
            'tomorrow'  =>   $data['tomorrow'],
        ];

        //活动推荐(轮播)
        $activityList = SystemGroupData::getAllValue("activity_recommend");
        foreach ($activityList as $k=>$v) {
            $v['image'] = str_replace("\\","/",$quota['website_domain'] . $v['image']);
            $activity[$k] = $v;
        }



        //任务列表
        $taskList = SystemGroupData::getAllValue("task_list");
        foreach ($taskList as $k=>$v) {

            switch ($v['value']) {
                case "open_notice":
                    if (User::where("uid",$uid)->where("open_notice",1)->count() > 0) {
                        $taskList[$k]['completed'] = 1;
                        if (UserDefinite::where("uid",$uid)->where("mold","tesk_".$v['value'])->count() > 0) {
                            $taskList[$k]['reward'] = 1;
                        }else{
                            $taskList[$k]['reward'] = 0;
                        }

                    }else{
                        $taskList[$k]['completed'] = 0;
                        $taskList[$k]['reward'] = 0;
                    }
                    break;
                case "basic_info":
                    if (UserBasic::where("uid",$uid)->where("marriage","<>",0)->where("address","<>","")->where("detail_address","<>","")->where("education","<>",0)->where("company_name","<>","")->where("company_address","<>","")->where("company_detail_address","<>","")->count() > 0) {
                        $taskList[$k]['completed'] = 1;
                        if (UserDefinite::where("uid",$uid)->where("mold","tesk_".$v['value'])->count() > 0) {
                            $taskList[$k]['reward'] = 1;
                        }else{
                            $taskList[$k]['reward'] = 0;
                        }

                    }else{
                        $taskList[$k]['completed'] = 0;
                        $taskList[$k]['reward'] = 0;
                    }
                    break;
                default:
                    if (UserDefinite::where("uid",$uid)->where("mold","tesk_".$v['value'])->count() > 0) {
                        $taskList[$k]['completed'] = 1;
                        $taskList[$k]['reward'] = 1;
                    }else{
                        $taskList[$k]['completed'] = 0;
                        $taskList[$k]['reward'] = 0;
                    }
                    break;
            }
        }
        return app("json")->success(compact("signList","signinfo","activityList","taskList"));
    }

    //签到
    public function sign(Request $request)
    {
        $paramGold = Util::postMore([
            ['gold',0],
            ['type','']
        ]);

        $uid = $request->uid();
        if (isset($paramGold['gold']) && $paramGold['gold'] != 0) {
            $res = Db::name("user")->where("uid",$uid)->inc("gold",$paramGold['gold']);
            $signInfo = [
                'uid'     =>  $uid,
                'type'    =>  0,
                'mold'    =>  'tesk_'.$paramGold['type'],
                'number'  =>  'gold',
                'pm'      =>  1,
                'create_time' => time(),
                'title'    =>  '签到'
            ];
            $res1 = UserDefinite::create($signInfo);
            return $res ? app("json")->success("任务已完成") : app("json")->fail("操作失败");
        }

        //判断用户今天是否签到
        if (Db::name("user_definite")->where("uid",$uid)->where("mold","sign")->where("type",0)->where("pm",1)->whereDay("create_time")->count()>0)  return app("json")->fail("今日已签到");
       

        $data = self::signRes($uid);

        //设置签到

        $signInfo = [
            'uid'     =>  $uid,
            'type'    =>  0,
            'mold'    =>  'sign',
            'number'  =>  $data['tomorrow'],
            'pm'      =>  1,
            'create_time' => time(),
            'title'    =>  '签到'
        ];

        Db::name("user")->where("uid",$uid)->inc("gold",$data['tomorrow']);
        $res = UserDefinite::create($signInfo);
        return $res ? app("json")->success("签到成功") : app("json")->fail("签到失败");
    }



    /**
     * 签到详情
     */
    public  function signRes($uid)
    {
        $data = array();
        $number = 0;
        //是否签到
        $is_sign  = Db::name("user_definite")->where("uid",$uid)->where("mold","sign")->where("type",0)->where("pm",1)->whereDay("create_time")->count();
        if ($is_sign>0) {
            $data['is_sign'] = 1;
            $number = 1;
        }else{
            $data['is_sign'] = 0;
        }


        //获取签到配置
        $signConfig = SystemConfigMore(['sign_periods_days','sign_initial_gold','sign_gold_add_number']);
        $signList = [(int)$signConfig['sign_initial_gold']];
        
        //设置签到
        for ($i=1;$i<$signConfig['sign_periods_days'];$i++) {
            $signList[$i] = $signConfig['sign_initial_gold']+$i*$signConfig['sign_gold_add_number'];
        }

        //获取用户签到了几次
        $pastList = UserDefinite::where("uid",$uid)->where("mold","sign")->where("type",0)->where("pm",1)->where("create_time","<",mktime(0,0,0,date('m'),date('d'),date('Y')))->order("create_time desc")->select();
        if ($pastList) {
            $pastList = $pastList->toArray();
        
            for ($i=0;$i<count($pastList);$i++) {
                $pastList[$i]['create_time'] = strtotime($pastList[$i]['create_time']);
                $beginYesterday=mktime(0,0,0,date('m'),date('d')-($i+1),date('Y'));
                $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-($i+1);
                if ($pastList[$i]['create_time'] >= $beginYesterday && $pastList[$i]['create_time'] <= $endYesterday) {
                    $number++;
                }else{
                    break;
                }

            }
        }



        $number = fmod(floatval($number),(int)$signConfig['sign_periods_days']);

        $data['signList'] = $signList;
        $data['number'] = (int)$number;

        $data['tomorrow'] = (int)$signList[$number];

        return $data;
    }
    
}
